﻿using System.Collections;
using System.Collections.Generic;
using System.Reflection.Metadata;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Npgsql;
using sixgod.Base.Controllers;
using sixgod.Dtos.System;
using sixgod.Model.System;
using sixgod.Model.System.Dictionary;
using sixgod.Service.Dictionary;

namespace sixgod.Module.SystemCore.Controllers.Dictionary
{
    [Route("[Controller]")]
    public class DictionaryController:BaseController
    {
        private IDictionaryService _service;
        private IMapper _mapper;
        public DictionaryController(IDictionaryService service, IMapper mapper)
        {
            _service = service;
            _mapper = mapper;
        }

        [HttpGet("{code}")]
        public async Task<IActionResult> GetDicList(string code)
        {
            var list = await _service.GetDicList(code);
            return Success(list);
        }

        [HttpGet("page")]
        public async Task<IActionResult> GetPageList([FromQuery]DicQueryModel search)
        {
            var result = await _service.GetPageList(search);
            return Success(result);
        }

        /// <summary>
        /// 用于下拉框使用
        /// </summary>
        /// <returns></returns>
        [HttpGet("select")]
        public async Task<IActionResult> GetListForSelect()
        {
            var list = await _service.GetList();
            return Success(_mapper.Map<List<SelectDto>>(list));
        }


        #region 字典类型增删改

        [HttpPost]
        public async Task<IActionResult> Insert([FromBody]SystemDictionary dictionary)
        {
            var vaild = dictionary.Verify();
            if (!vaild.Item1)
            {
                return Fail(vaild.Item2);
            }
            dictionary.Create("admin");

            try
            {
                await _service.InsertSystemDictionaryAsync(dictionary);
                return Success("OK");
            }
            catch (PostgresException e)
            {
                if (e.Code=="23505" && e.ConstraintName=="unique_type")
                {
                    return Fail("字典类型重复！");
                }
                return Fail(e.MessageText);
            }
        }

        [HttpPut]
        public async Task<IActionResult> Update([FromBody]SystemDictionary dictionary)
        {
            dictionary.Update("admin");
            try
            {
                await _service.UpdateSystemDictionaryAsync(dictionary);
                return Success("OK");
            }
            catch (PostgresException e)
            {
                if (e.Code=="23505" && e.ConstraintName=="unique_type")
                {
                    return Fail("字典类型重复！");
                }
                return Fail(e.MessageText);
            }
        }

        [HttpDelete("{code}")]
        public async Task<IActionResult> Delete(string code)
        {
            await _service.DeleteSystemDictionaryAsync(code);
            return Success("OK");
        }

        #endregion

        #region 字典项增删改

        [HttpPost("item")]
        public async Task<IActionResult> InsertItem([FromBody]DictionaryItem item)
        {
            var vaild = item.Verify();
            if (!vaild.Item1)
            {
                return Fail(vaild.Item2);
            }
            item.Create("admin");

            try
            {
                await _service.InsertSystemDictionaryItemAsync(item);
                return Success("OK");
            }
            catch (Exception e)
            {
                
                return Fail(e.Message);
            }
        }

        [HttpPut("item")]
        public async Task<IActionResult> UpdateItem([FromBody]DictionaryItem item)
        {
            item.Update("admin");
            try
            {
                await _service.UpdateSystemDictionaryItemAsync(item);
                return Success("OK");
            }
            catch (Exception e)
            {
                return Fail(e.Message);
            }
        }

        [HttpDelete("item/{code}")]
        public async Task<IActionResult> DeleteItem(string code)
        {
            await _service.DeleteSystemDictionaryItemAsync(code);
            return Success("OK");
        }

        #endregion
    }
}
